package com.qf.staff.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.common.base.exception.BaseException;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.ResultCode;
import com.qf.common.db.utils.FileUploadUtils;
import com.qf.common.db.utils.PageCommonBeanUtils;
import com.qf.staff.common.qo.EmpQo;
import com.qf.staff.common.utils.CommonBeanUtils;
import com.qf.staff.common.vo.EmpVo;
import com.qf.staff.mapper.EmpMapper;
import com.qf.staff.pojo.Emp;
import com.qf.staff.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class EmpServiceImpl implements EmpService {
    @Resource
    EmpMapper empMapper;
    @Resource
    private FileUploadUtils fileUploadUtils;

    @Override
    public IPage<EmpVo> list(int page, int size, String empName) {
        try {
            QueryWrapper<Emp> lqw = new QueryWrapper<>();
            if (empName != null) {
                lqw.eq("emp_name", empName);
            }
            return PageCommonBeanUtils.copyPage(empMapper.selectPage(new Page<>(page, size), lqw),
                    new Page<>(), EmpVo::new);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
    }

    @Override
    public int delete(EmpQo empQo) {
        try {
            Emp emp = new Emp();
            BeanUtils.copyProperties(empQo, emp);
            return empMapper.updateByStatus(emp);
        } catch (BeansException e) {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        }
    }

    @Transactional
    @Override
    public int deleteAll(List<Integer> ids, int status) {
        try {
            int num = empMapper.updateByAllStatus(ids, status);
            if (num < ids.size()) {
                throw new ServiceException(ResultCode.DELETE_ERROR);
            }
            return num;
        } catch (Exception e) {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        }
    }


    @Override
    public int modify(EmpQo empQo) {
        try {
            Emp emp = new Emp();
            BeanUtils.copyProperties(empQo, emp);
            return empMapper.updateById(emp);
        } catch (BeansException e) {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        }
    }

    @Override
    public int add(EmpQo empQo, MultipartFile file) {
        Emp emp = new Emp();

        BeanUtils.copyProperties(empQo, emp);
        try {
            String s = fileUploadUtils.uploadImage(file);
            if (!ObjectUtils.isEmpty(s)) {
                emp.setHeadPortrait(s);
                return empMapper.insert(emp);
            } else {
                throw new ServiceException(ResultCode.IMG_NOT_EXIST);
            }
        } catch (RuntimeException e) {
            throw new BaseException(ResultCode.IMG_NOT_EXIST);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }

    @Override
    public PageInfo<EmpVo> listEmp(int page, int size) {
        try {
            return CommonBeanUtils.copyPageInfo(PageHelper.startPage(page, size).doSelectPageInfo(() ->
                    empMapper.selectEmp()), new PageInfo<>(), EmpVo::new);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
    }


}
